package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    // 模拟用户数据库
    private final String VALID_USERNAME = "admin";
    private final String VALID_PASSWORD = "123456";

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if(VALID_USERNAME.equals(username) && VALID_PASSWORD.equals(password)) {
            // 创建用户信息对象
            User user = new User(username, "物联网工程", "2022级", "3班", "130-XXXX-XXXX","重庆科技大学","四川达州");
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            response.sendRedirect("info.jsp");
        } else {
            request.setAttribute("error", "用户名或密码错误");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }
}